package cn.wjzhang.middle.modules.area.service.impl;

import cn.wjzhang.core.constant.DelConstant;
import cn.wjzhang.core.exception.BootException;
import cn.wjzhang.middle.modules.area.beans.vo.MidStreetsVo;
import cn.wjzhang.middle.modules.area.beans.entity.MidStreets;
import cn.wjzhang.middle.modules.area.mapper.MidStreetsMapper;
import cn.wjzhang.middle.modules.area.beans.request.MidStreetsAddRequest;
import cn.wjzhang.middle.modules.area.beans.request.MidStreetsEditRequest;
import cn.wjzhang.middle.modules.area.beans.request.MidStreetsListRequest;
import cn.wjzhang.middle.modules.area.service.MidStreetsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 乡级（乡镇、街道）Service实现层
 *
 * @author wjzhang
 * @date 2023-06-28 18:48:28
 */
@Service
public class MidStreetsServiceImpl extends ServiceImpl<MidStreetsMapper, MidStreets> implements MidStreetsService {

    @Resource
    private MidStreetsMapper midStreetsMapper;

    @Override
    public void removeByIdLogic(Long id) {
        MidStreets entity = this.getById(id);
        if (Objects.isNull(entity) || entity.getDelFlag() == 1) {
            throw new BootException("查询对象不存在或者已经被删除");
        }
        entity.setDelFlag(DelConstant.DEL);
        this.updateById(entity);
    }

    @Override
    public IPage<MidStreetsVo> listWithPage(MidStreetsListRequest param) {
        Page<MidStreetsVo> page = new Page<>(param.getPageNo(), param.getPageSize());
        List<MidStreetsVo> list = midStreetsMapper.listWithPage(page, param);
        return page.setRecords(list);
    }

    @Override
    public MidStreetsVo getDTO(Long id) {
        MidStreets entity = this.getById(id);
        if (Objects.isNull(entity) || entity.getDelFlag() == 1) {
            throw new BootException("查询对象不存在或者已经被删除");
        }
        MidStreetsVo vo = new MidStreetsVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = {Throwable.class})
    public Long addMidStreets(MidStreetsAddRequest addRequest) {
        MidStreets entity = new MidStreets();
        BeanUtils.copyProperties(addRequest, entity);
        this.save(entity);
        return entity.getId();
    }

    @Override
    @Transactional(rollbackFor = {Throwable.class})
    public void editMidStreets(MidStreetsEditRequest editRequest) {
        MidStreets entity = this.getById(editRequest.getId());
        if (Objects.isNull(entity) || entity.getDelFlag() == 1) {
            throw new BootException("查询对象不存在或者已经被删除");
        }
        BeanUtils.copyProperties(editRequest, entity);
        this.updateById(entity);
    }
}
